package com.wanglibao.woodtrade.woodtrade.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.InformationList;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.activity.InformationDetailActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.ItemLvInformationAvtivityAdapter;
import com.wanglibao.woodtrade.woodtrade.util.PullUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshBase;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 资讯
 * Created by lugg on 2016/5/10.
 */
public class InformationFragment extends BaseFragment implements  AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2{

    @Bind(R.id.tv_commonTitle_title)
    TextView mTvCommonTitleTitle;
    @Bind(R.id.imgbtn_commonTitle_back)
    ImageButton mImgbtnCommonTitleBack;
    @Bind(R.id.tv_commonTitle_right)
    TextView mTvCommonTitleRight;
    @Bind(R.id.rl_common_title)
    RelativeLayout rl_common_title;
    @Bind(R.id.lv_information_activity)
    PullToRefreshListView mLvInformationActivity;
    //==================================
    InformationList mInformationList = null;
    Context mContext;
    int PageNumber=1;
    ItemLvInformationAvtivityAdapter mAdapter;
    List<InformationList.ListEntity> mEntityList;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 == 2) {
                if (PageNumber == 1) {
                    if (mInformationList != null && mEntityList != null) {
                        mEntityList.clear();
                    }
                    mAdapter.setList(mEntityList);
                } else {
                    ToastUtil.createLongToast(getContext(), "没有更多数据了");
                    PageNumber--;
                }
            } else {
                InformationList mInformationList1 = new Gson().fromJson(msg.obj.toString(), InformationList.class);
                if (PageNumber == 1) {
                    mInformationList = mInformationList1;
                    mEntityList = mInformationList.getList();
                } else {
                    mEntityList.addAll(mInformationList1.getList());
                }
                mAdapter.setList(mEntityList);
            }
            mLvInformationActivity.onRefreshComplete();
            return false;
        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_information, container, false);
        ButterKnife.bind(this, view);
        mContext = getContext();
        if (Build.VERSION.SDK_INT < 19) {
            rl_common_title.setPadding(0, 0, 0, 0);
        }
//        updateTagLists();
        initData();
        initView();
        return view;
    }
    private void initView() {
        //通用 初始化
        mEntityList = new ArrayList<>();
        mAdapter = new ItemLvInformationAvtivityAdapter(getContext(), mEntityList);
        mLvInformationActivity.getRefreshableView().setAdapter(mAdapter);
        mLvInformationActivity.getRefreshableView().setOnItemClickListener(this);
        mLvInformationActivity.setMode(PullToRefreshBase.Mode.BOTH);
        mLvInformationActivity.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
        mLvInformationActivity.setOnRefreshListener(this);
        PullUtil.initIndicator(mLvInformationActivity);
    }

    public void refresh(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLvInformationActivity.setRefreshing();
            }
        }, 300);
    }

    public void updateTagLists() {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(false,this, NetConfig.SERVER + NetConfig.TYPE_NEW,
                NetConfig.newIndex, mHandler,
                "jsonObject", NetConfig.TaskId.newIndex, new Object[]{PageNumber});
        jsonRPCAsyncTask.execute(new Void[]{});
    }
    private void initData() {
        mImgbtnCommonTitleBack.setVisibility(View.GONE);
        mTvCommonTitleTitle.setText("资讯");
        mLvInformationActivity.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), InformationDetailActivity.class);
        intent.putExtra("Id",mEntityList.get(position-1).getId());
        startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        if ((mEntityList == null || mEntityList.size() ==0) && !mLvInformationActivity.isRefreshing()){
            updateTagLists();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        PageNumber=1;
        updateTagLists();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
       PageNumber++;
        updateTagLists();
    }
}
